/*
 * Copyright (c) 2020-2023 云南信和科技有限公司 All Rights Reserved.
 * 当前软件由云南信和科技有限公司研制开发，未经公司正式同意，其他任何个人、团体不得使用复制修改.
 * @author: yansheng
 */
package com.yuntu.micor.app.entity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;

/**
 * @author yansheng
 */
public class ResponseData<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code = 200;
    @Getter
    @Setter
    private String message = "";
    @Getter
    @Setter
    private T data;

    @Getter
    @Setter
    private boolean success = false;
    @Getter
    @Setter
    private long total = 0L;
    @Getter
    @Setter
    private long pageSize = 0L;
    @Getter
    @Setter
    private long current = 0L;


    private static <T> ResponseData<T> restResult(T data, int code, String message, boolean success, long total, long pageSize, long current) {
        ResponseData<T> apiResult = new ResponseData<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(message);
        apiResult.setSuccess(success);
        apiResult.setTotal(total);
        apiResult.setPageSize(pageSize);
        apiResult.setCurrent(current);
        return apiResult;
    }

    /**
     * 分页数据结果
     *
     * @param pageInfo 分页数据
     * @return 结果
     */
    public static <T> ResponseData<List<T>> page(IPage<T> pageInfo) {
        return restResult(pageInfo.getRecords(), ResponseCode.SUCCESS_CODE.getCode(), null, true, pageInfo.getTotal(), pageInfo.getSize(), pageInfo.getCurrent());
    }

    /**
     * 数组数据
     *
     * @return 结果
     */
    public static <T> ResponseData<List<T>> list(List<T> list) {
        return restResult(list, ResponseCode.SUCCESS_CODE.getCode(), null, true, list.size(), 0L, 0L);
    }

    /**
     * 树结构数据
     *
     * @param trees 树结构
     * @param total 节点总数
     * @return 结果
     */
    public static <T> ResponseData<T> tree(T trees, long total) {
        return restResult(trees, ResponseCode.SUCCESS_CODE.getCode(), null, true, total, 0L, 0L);

    }


    public static <T> ResponseData<T> ok(T data) {
        return restResult(data, ResponseCode.SUCCESS_CODE.getCode(), null, true, 0L, 0L, 0L);
    }

    public static <T> ResponseData<T> ok(String message, T data) {
        return restResult(data, ResponseCode.SUCCESS_CODE.getCode(), message, true, 0L, 0L, 0L);

    }

    public static <T> ResponseData<T> fail() {
        return restResult(null, ResponseCode.SERVER_ERROR_CODE.getCode(), null, false, 0L, 0L, 0L);
    }

    public static <T> ResponseData<T> fail(T data) {
        return restResult(data, ResponseCode.SERVER_ERROR_CODE.getCode(), null, false, 0L, 0L, 0L);
    }

    public static <T> ResponseData<T> fail(T data, String message) {
        return restResult(data, ResponseCode.SERVER_ERROR_CODE.getCode(), message, false, 0L, 0L, 0L);
    }

    public static <T> ResponseData<T> fail(String message) {
        return restResult(null, ResponseCode.SERVER_ERROR_CODE.getCode(), message, false, 0L, 0L, 0L);
    }

    public static <T> ResponseData<T> fail(String message, int code) {
        return restResult(null, code, message, false, 0L, 0L, 0L);
    }

    public static <T> ResponseData<T> fail(T data, String message, int code) {
        return restResult(data, code, message, false, 0L, 0L, 0L);
    }

}
